package com.supor.aiot.md_msg.model;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.android.baseconfig.base.BaseApiResult;
import com.android.baseconfig.common.config.HttpConstant;
import com.android.baseconfig.common.utils.SystemUtils;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.md_msg.api.MsgCenterApiImpl;
import com.supor.aiot.md_msg.entity.MsgCenterListResultBean;

import org.json.JSONException;
import org.json.JSONObject;

import autodispose2.AutoDisposeConverter;
import io.reactivex.rxjava3.functions.Consumer;
import okhttp3.RequestBody;

/**
 * @author: zook
 * @date: 2021/10/13
 */
public class MsgCenterModel extends BaseViewModel {

    private MutableLiveData<Result<MsgCenterListResultBean>> systemMsgUnReadResult = new MutableLiveData<>();
    private MutableLiveData<Result<MsgCenterListResultBean>> systemMsgListResult = new MutableLiveData<>();
    private MutableLiveData<Result<MsgCenterListResultBean>> replyMsgListResult = new MutableLiveData<>();
    private MutableLiveData<Result<MsgCenterListResultBean>> likeMsgListResult = new MutableLiveData<>();
    private MutableLiveData<Result<String>> cleanMsgResult = new MutableLiveData<>();
    private MutableLiveData<Result<String>> delAllMsgResult = new MutableLiveData<>();
    private MutableLiveData<Result<String>> msgReadResult = new MutableLiveData<>();
    private MutableLiveData<Result<MsgCenterListResultBean>> replyMsgListUnreadNumResult = new MutableLiveData<>();
    private MutableLiveData<Result<MsgCenterListResultBean>> likeMsgListUnreadNumResult = new MutableLiveData<>();

    public MsgCenterModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<Result<MsgCenterListResultBean>> getSystemMsgListResult() {
        return systemMsgListResult;
    }

    public void getSystemMsgList(String bossUserId,
                                 int page,
                                 AutoDisposeConverter<BaseApiResult<MsgCenterListResultBean>> autoDisposeConverter
    ) {
        MsgCenterApiImpl.getInstance().getMsgList(bossUserId,
                "SYSTEM",
                "CREATE_TIME%20DESC",
                20, page)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<MsgCenterListResultBean>>() {
                    @Override
                    public void accept(BaseApiResult<MsgCenterListResultBean> msgCenterListBeanBaseApiResult) throws Exception {
                        systemMsgListResult.setValue(new Result.Success<>(msgCenterListBeanBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        systemMsgListResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public void getReplyMsgList(String bossUserId,
                                int page,
                                AutoDisposeConverter<BaseApiResult<MsgCenterListResultBean>> autoDisposeConverter
    ) {
        MsgCenterApiImpl.getInstance().getReplyMsgList(bossUserId,
                "REPLY",
                "COMMUNITY",
                "CREATE_TIME%20DESC",
                20, page)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<MsgCenterListResultBean>>() {
                    @Override
                    public void accept(BaseApiResult<MsgCenterListResultBean> msgCenterListBeanBaseApiResult) throws Exception {
                        replyMsgListResult.setValue(new Result.Success<>(msgCenterListBeanBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        replyMsgListResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public void getLikeMsgList(String bossUserId,
                               int page,
                               AutoDisposeConverter<BaseApiResult<MsgCenterListResultBean>> autoDisposeConverter
    ) {
        MsgCenterApiImpl.getInstance().getLikeMsgList(bossUserId,
                "LIKE",
                "COMMUNITY",
                "CREATE_TIME%20DESC",
                20, page)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<MsgCenterListResultBean>>() {
                    @Override
                    public void accept(BaseApiResult<MsgCenterListResultBean> msgCenterListBeanBaseApiResult) throws Exception {
                        likeMsgListResult.setValue(new Result.Success<>(msgCenterListBeanBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        likeMsgListResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public void getSystemMsgListCount(String bossUserId,
                                      int isRead,//0：未读；1：已读
                                      AutoDisposeConverter<BaseApiResult<MsgCenterListResultBean>> autoDisposeConverter
    ) {
        MsgCenterApiImpl.getInstance().getMsgListUnReadCount(bossUserId,
                "SYSTEM",
                "CREATE_TIME%20DESC",
                0,
                99, 1)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<MsgCenterListResultBean>>() {
                    @Override
                    public void accept(BaseApiResult<MsgCenterListResultBean> msgCenterListBeanBaseApiResult) throws Exception {
                        systemMsgUnReadResult.setValue(new Result.Success<>(msgCenterListBeanBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        systemMsgUnReadResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public void cleanAllRead(AutoDisposeConverter<BaseApiResult<String>> autoDisposeConverter) {
        MsgCenterApiImpl.getInstance().cleanRead(1)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<String>>() {
                    @Override
                    public void accept(BaseApiResult<String> stringBaseApiResult) throws Exception {
                        cleanMsgResult.setValue(new Result.Success<>(stringBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        cleanMsgResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public void delAllMsg(AutoDisposeConverter<BaseApiResult<String>> autoDisposeConverter) {
        MsgCenterApiImpl.getInstance().delAllMsg(1)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<String>>() {
                    @Override
                    public void accept(BaseApiResult<String> stringBaseApiResult) throws Exception {
                        delAllMsgResult.setValue(new Result.Success<>(stringBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        delAllMsgResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public void readMsg(String id, AutoDisposeConverter<BaseApiResult<String>> autoDisposeConverter) {
        JSONObject jsonObject = new JSONObject();
        RequestBody requestBody = null;
        try {
//            {"autoId":"123","isRead":1}
            jsonObject.put("autoId", id);
            jsonObject.put("isRead", 1);
            requestBody = RequestBody.create(HttpConstant.JSON,
                    jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MsgCenterApiImpl.getInstance().msgRead(id, requestBody)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<String>>() {
                    @Override
                    public void accept(BaseApiResult<String> stringBaseApiResult) throws Exception {
                        msgReadResult.setValue(new Result.Success<>(stringBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        msgReadResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }


    public void getReplyMsgListUnreadNum(String bossUserId,
                                         AutoDisposeConverter<BaseApiResult<MsgCenterListResultBean>> autoDisposeConverter
    ) {
        MsgCenterApiImpl.getInstance().getReplyMsgListUnreadNum(bossUserId,
                "LIKE",
                "COMMUNITY",
                "CREATE_TIME%20DESC",
                0,
                99, 1)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<MsgCenterListResultBean>>() {
                    @Override
                    public void accept(BaseApiResult<MsgCenterListResultBean> msgCenterListBeanBaseApiResult) throws Exception {
                        replyMsgListUnreadNumResult.setValue(new Result.Success<MsgCenterListResultBean>(msgCenterListBeanBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        replyMsgListUnreadNumResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public void getLikeMsgListUnreadNum(String bossUserId,
                                        AutoDisposeConverter<BaseApiResult<MsgCenterListResultBean>> autoDisposeConverter
    ) {
        MsgCenterApiImpl.getInstance().getLikeMsgListUnreadNum(bossUserId,
                "LIKE",
                "COMMUNITY",
                "CREATE_TIME%20DESC",
                0,
                99, 1)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<MsgCenterListResultBean>>() {
                    @Override
                    public void accept(BaseApiResult<MsgCenterListResultBean> msgCenterListBeanBaseApiResult) throws Exception {
                        likeMsgListUnreadNumResult.setValue(new Result.Success<>(msgCenterListBeanBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        likeMsgListUnreadNumResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public void delMsg(String id, AutoDisposeConverter<BaseApiResult<String>> autoDisposeConverter) {
        MsgCenterApiImpl.getInstance().delMsg(id)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<String>>() {
                    @Override
                    public void accept(BaseApiResult<String> stringBaseApiResult) throws Exception {

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }


    public MutableLiveData<Result<MsgCenterListResultBean>> getReplyMsgListResult() {
        return replyMsgListResult;
    }

    public MutableLiveData<Result<MsgCenterListResultBean>> getLikeMsgListResult() {
        return likeMsgListResult;
    }

    public MutableLiveData<Result<MsgCenterListResultBean>> getReplyMsgListUnreadNumResult() {
        return replyMsgListUnreadNumResult;
    }

    public MutableLiveData<Result<MsgCenterListResultBean>> getLikeMsgListUnreadNumResult() {
        return likeMsgListUnreadNumResult;
    }

    public MutableLiveData<Result<String>> getCleanMsgResult() {
        return cleanMsgResult;
    }

    public MutableLiveData<Result<String>> getDelAllMsgResult() {
        return delAllMsgResult;
    }

    public MutableLiveData<Result<String>> getMsgReadResult() {
        return msgReadResult;
    }
}